var Weixin = {
	init: function(appid, cb) {
		var info = uexWeiXin.registerApp(appid);
		if(info == 1) {
			return uexWindow.alert("提示", "微信授权失败!", "确定");
		}
		
		if(!uexWeiXin.isWXAppInstalled()) {
			return uexWindow.alert("提示", "请安装最新版微信!", "确定");
		}
		
		//微信登陆
		var params = {
			scope: "snsapi_userinfo,snsapi_base",
			state: "0710"
		};

		uexWeiXin.login(JSON.stringify(params), function(data) {
			if(data.errCode != 0) {
				return uexWindow.alert("提示", "微信授权失败!", "确定");
			}
			cb && cb(data.code);
		});
	},
	pay: function(obj, cb) {
		if(!uexWeiXin.isSupportPay()) {
			return uexWindow.alert("提示", "请安装最新版微信!", "确定");
		}
		
		var params = {
			appid: obj.appid,
	        partnerid: obj.partnerid,
	        prepayid: obj.prepayid,
	        package: obj.package,
	        noncestr: obj.noncestr,
	        timestamp: obj.timestamp,
	        sign: obj.sign
		}
		uexWeiXin.startPay(JSON.stringify(params), function(data) {
			if(data.errCode == 0) {
				cb && cb();
			} else if(data.errCode == -1){
				toast('支付失败');
			} else if(data.errCode == -2) {
				console.log('取消支付');
			}
			
		});
	},
	init2: function(appid, secret) {

		var info = uexWeiXin.registerApp(appid);
		//alert("注册:"+info);
		if(info == 1) {
			return uexWindow.alert("提示", "微信授权失败!", "确定");
		}
		//微信登陆
		var params = {
			scope: "snsapi_userinfo,snsapi_base",
			state: "0710"
		};
		var data = JSON.stringify(params);
		uexWeiXin.login(data, function(data) {
			//JPUSH_DEBUG = false;
			//alert("微信登陆:" + JSON.stringify(data));
			if(data.errCode != 0) {
				return uexWindow.alert("提示", "微信授权失败!", "确定");
			}
			//获取access_token
			var params1 = {
				secret: secret,
				code: data.code,
				grant_type: "authorization_code"
			};
			var data1 = JSON.stringify(params1);
			uexWeiXin.getLoginAccessToken(data1, function(data1) {
				//alert("access_token:" + JSON.stringify(data1));

				//获取用户个人信息
				var params2 = {
					access_token: data1.access_token,
					openid: data1.openid
				};
				var data2 = JSON.stringify(params2);
				uexWeiXin.getLoginUnionID(data2, function(data2) {
					//alert("个人信息:" + JSON.stringify(data2));
					var obj = {
						openId: data2.openid
					};
					appcan.request.ajax({
						url: server_path + '/app/app_otthLogin/otthLogin.do',
						type: 'POST',
						dataType: 'json',
						data: JSON.stringify(obj),
						beforeSend: function(xhr, settings) {
							//uexWindow.toast("0", "5", "登陆中", "0");
						},
						success: function(data) {
							//alert(JSON.stringify(data));
							if(data.code != 0) {
								confirm2(function() {}, data.codemsg, '提示', ['确定']);
								return;
							}
							//登陆成功
							LS.set("currentUser", data.result.results);
							//设置统计事件
							var eventName = "login";
							var dataYM = {
								"phone": data.result.results.mobilePhoneNo,
								"loginTime": data.result.results.mobilePhoneNo + " " + (new Date()).pattern("yyyy-MM-dd HH:mm")
							};
							uexUmeng.onEvent(eventName, JSON.stringify(dataYM));
							appcan.window.publish('reload', data.result.results);
							appcan.window.publish('reloadIndex', data.result.results);
							appcan.window.publish('reloadPortfolioList', data.result.results);
							appcan.window.publish('reloadIndexMessage', data.result.results);
							if(data.result.results && data.result.results.accountId) {
								public_user.payDefault();
							};
							//设置极光推送别名
							if(uexWidgetOne.platformName != "Simulator") {
								var tags = new Array("lclm");
								JPush.setAliasAndTags("lclm" + data.result.results.id, tags);
							}
							closeWin();
						},
						error: function(errMessage) {
							//alert("errMessage"+errMessage);
						}
					});

				});

			});

		});

	},
	//sid 共享内容ID  url 共享地址  title 共享标题   description 共享简介
	shareWeixin: function(index, sid, url, title, description, imgUrl) {
		//alert("点击了第"+(index)+"个按钮");
		if(index == "0") {
			var sharePath = url + sid + ".html";
			if(sid == 0) {
				sharePath = url;
			}
			if(!imgUrl) {
				imgUrl = "res://icon.png";
			}
			var info = uexWeiXin.registerApp(WEIXIN_APPID);
			//alert("分享注册:"+info);
			if(info == 1) {
				return uexWindow.alert("提示", "微信授权失败!", "确定");
			}
			var JsonData = '{"thumbImg":"' + imgUrl + '","wedpageUrl":"' + sharePath + '","scene":0,"title":"' + title + '","description":"' + description + '"}';
			uexWeiXin.shareLinkContent(JsonData, function(data) {
				if(data != 0) {
					return uexWindow.alert("提示", "分享失败!", "确定");
				}
				return uexWindow.alert("提示", "分享成功!", "确定");
			});
		} else if(index == "1") {
			var sharePath = url + sid + ".html";
			if(sid == 0) {
				sharePath = url;
			}
			if(!imgUrl) {
				imgUrl = "res://icon.png";
			}
			var info = uexWeiXin.registerApp(WEIXIN_APPID);
			//alert("注册:"+info);
			if(info == 1) {
				return uexWindow.alert("提示", "微信授权失败!", "确定");
			}
			var JsonData = '{"thumbImg":"' + imgUrl + '","wedpageUrl":"' + sharePath + '","scene":1,"title":"' + title + '","description":"' + description + '"}';
			uexWeiXin.shareLinkContent(JsonData, function(data) {
				if(data != 0) {
					return uexWindow.alert("提示", "分享失败!", "确定");
				}
				return uexWindow.alert("提示", "分享成功!", "确定");
			});
		}

	}

}